/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

namespace GrooveWebServicesV12Helpers.Forms
{
    public class FormFileAttachment
    {
        private System.Data.DataRow m_FileAttachmentDataRow;
        private byte[] m_Contents;

        static public GrooveWebServicesV12Helpers.Forms.FormFileAttachment[] GetFormFileAttachments(System.Data.DataRow i_DataRow)
        {
            System.Collections.Generic.List<GrooveWebServicesV12Helpers.Forms.FormFileAttachment> FormFileAttachmentList = new System.Collections.Generic.List<FormFileAttachment>();

            //  Check to make sure attachments exists
            if (i_DataRow.Table.ChildRelations.Count > 0)
            {
                //  Get attachments data row based on child relation
                System.Data.DataRow[] ChildAttachmentsDataRows = i_DataRow.GetChildRows(i_DataRow.Table.ChildRelations[0]);
                if (ChildAttachmentsDataRows.Length == 0)
                    throw new System.ArgumentException("Invalid datarow - missing Attachments child row!");

                System.Data.DataRow ChildAttachmentsDataRow = ChildAttachmentsDataRows[0];

                //  Get file attachment data row based on child relation
                System.Data.DataRow[] ChildFileAttachmentDataRows = ChildAttachmentsDataRow.GetChildRows(ChildAttachmentsDataRow.Table.ChildRelations[0]);

                if (ChildFileAttachmentDataRows.Length > 0)
                {
                    foreach (System.Data.DataRow ChildFileAttachmentDataRow in ChildFileAttachmentDataRows)
                    {
                        GrooveWebServicesV12Helpers.Forms.FormFileAttachment FormFileAttachment = new GrooveWebServicesV12Helpers.Forms.FormFileAttachment(ChildFileAttachmentDataRow);
                        FormFileAttachmentList.Add(FormFileAttachment);
                    }
                }
            }

            return FormFileAttachmentList.ToArray();
        }

        private FormFileAttachment(System.Data.DataRow i_FileAttachmentDataRow)
        {
            m_FileAttachmentDataRow = i_FileAttachmentDataRow;

            // Get contents of attachment, if any were read 
            // (reader must set i_IncludeFileAttachmentContent true to get attachment data)
            System.Data.DataRow[] ChildRows = m_FileAttachmentDataRow.GetChildRows("FileAttachment_Contents");
            if (ChildRows.Length > 0)
            {
                System.Data.DataRow ChildContentsDataRow = ChildRows[0];
                m_Contents = (byte[])ChildContentsDataRow["Base64"];
            }
        }

        public string Name
        {
            get
            {
                return m_FileAttachmentDataRow["Name"].ToString();
            }
        }

        public System.DateTime Modified
        {
            get
            {
                return System.Convert.ToDateTime(m_FileAttachmentDataRow["Modified"]);
            }
        }

        public string AttachmentsID
        {
            get
            {
                return m_FileAttachmentDataRow["Attachments_Id"].ToString();
            }
        }

        public string FileAttachmentID
        {
            get
            {
                return m_FileAttachmentDataRow["FileAttachment_Id"].ToString();
            }
        }

        public int Size
        {
            get
            {
                return System.Convert.ToInt32(m_FileAttachmentDataRow["Size"]);
            }
        }

        public string DisplayName
        {
            get
            {
                return m_FileAttachmentDataRow["DisplayName"].ToString();
            }
        }

        public System.DateTime Created
        {
            get
            {
                return System.Convert.ToDateTime(m_FileAttachmentDataRow["Created"]);
            }
        }

        public string CreatedBy
        {
            get
            {
                return m_FileAttachmentDataRow["CreatedBy"].ToString();
            }
        }

        public string ModifiedBy
        {
            get
            {
                return m_FileAttachmentDataRow["ModifiedBy"].ToString();
            }
        }

        public string Type
        {
            get
            {
                return m_FileAttachmentDataRow["Type"].ToString();
            }
        }

        public byte[] Contents
        {
            get
            {
                return m_Contents;
            }
        }
    }
}
